package com.goods.controller.busniess;

import com.github.pagehelper.IPage;
import com.github.pagehelper.PageInfo;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description：
 * @version:
 * @author：ruixinag
 * @create：Created in 2021/11/8 14:09
 */
@Api(tags = "业务模块-物资类别 ")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


    //类别列表展示
    @ApiOperation("类别列表展示")
    @GetMapping("/categoryTree")
    public ResponseBean<Map<String, Object>> categoryTree(@RequestParam Map paramMap) {
        //获取分页数据
        PageInfo<ProductCategoryTreeNodeVO> pageInfo = productCategoryService.categoryTree(paramMap);
        Map<String, Object> map = new HashMap();

        map.put("rows", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return ResponseBean.success(map);
    }

    //获取父分类
    @ApiOperation("获取父分类")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> list = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(list);
    }

    //添加分类
    @ApiOperation("添加分类")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategory productCategory) {
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryService.add(productCategory);
        return ResponseBean.success();
    }

    //删除分类,有子分类的不能删除
    @ApiOperation("删除分类")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(
            @PathVariable Long id
    ) {
        boolean isDelete = productCategoryService.deleteCategoryById(id);
        if (isDelete) {
            return ResponseBean.success();
        } else {
            Map map = new HashMap();
            map.put("errorMsg", "有子分类不允许删除!");
            return ResponseBean.error(map);
        }

    }

    //根据id获取分类信息
    @ApiOperation("根据id获取分类信息")
    @GetMapping("/edit/{id}")
    public ResponseBean<ProductCategory> edit(@PathVariable Long id) {
        ProductCategory productCategory = productCategoryService.editCategoryById(id);
        return ResponseBean.success(productCategory);
    }

    //更新分类信息
    @ApiOperation("更新分类信息 ")
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody ProductCategory productCategory) {
        productCategoryService.updateCategory(productCategory);
        return ResponseBean.success();
    }


}
